//
//  JQSearchView.swift
//  sinaWeibo
//
//  Created by maoge on 16/4/16.
//  Copyright © 2016年 maoge. All rights reserved.
//

import UIKit

class JQSearchView: UIView {

    @IBOutlet weak var RightConstraint: NSLayoutConstraint!
    @IBOutlet weak var textfield: UITextField!
    @IBOutlet weak var cancelBtn: UIButton!
   
    
    class func searchView() -> JQSearchView {
        
        let view = NSBundle.mainBundle().loadNibNamed("JQSearchView", owner: nil, options: nil).last as! JQSearchView
        
        return view
    }
    
    //开始编辑
    @IBAction func textchange(sender: UITextField) {
        
        RightConstraint.constant = 50
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.textfield.layoutIfNeeded()
        }
    }
    
    //取消按钮
    @IBAction func cancelBtnClick(sender: AnyObject) {
        
        RightConstraint.constant = 0
        self.endEditing(false)
        
        //执行动画
        UIView.animateWithDuration(0.25) { () -> Void in
            self.textfield.layoutIfNeeded()
        }
        
    }
    
    override func awakeFromNib() {
        
        leftImage.frame = CGRect(x: 0, y: 0, width: self.frame.size.height, height: self.frame.size.height)
        
        textfield.leftView = leftImage
        
        textfield.leftViewMode = .Always
        
        //设置边框
        textfield.layer.borderWidth = 5
        textfield.layer.borderColor = UIColor.greenColor().CGColor
        textfield.layer.cornerRadius = 5
        textfield.layer.masksToBounds = true
        
    }
    
    //设置放大镜视图
    lazy var leftImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        image.contentMode = .Center
        
        return image
    }()

}
